﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label6.Visible = false;
            label9.Visible = false;
            if (n > 0&&(color=="red"||color=="blue"||color=="yellow"||color=="green"||color=="black"))
            {
                if (graphics == null) graphics = this.CreateGraphics();
                drawCayleyTree(n, 200, 310, leng, -Math.PI / 2,color);
            }
            if(n<=0)
            {
                label6.Visible = true;
            }
            if(color==null)
            {
                label9.Visible = true;
            }
        }

        private Graphics graphics;
        double th1;
        double th2;
        double per1;
        double per2;
        int n;
        double leng;
        string color;



        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th,string drawcolor)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1,drawcolor);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1,drawcolor);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2,drawcolor);
        }
        void drawLine(double x0, double y0, double x1, double y1,string drawcolor)
        {
            if (drawcolor.Equals("red"))
            {
                graphics.DrawLine(
                    Pens.Red,
                    (int)x0, (int)y0, (int)x1, (int)y1);
            }
            else if (drawcolor.Equals("blue"))
            {
                graphics.DrawLine(
                    Pens.Blue,
                    (int)x0, (int)y0, (int)x1, (int)y1);
            }
            else if (drawcolor.Equals("green"))
            {
                graphics.DrawLine(
                    Pens.Green,
                    (int)x0, (int)y0, (int)x1, (int)y1);
            }
            else if (drawcolor.Equals("yellow"))
            {
                graphics.DrawLine(
                    Pens.Yellow,
                    (int)x0, (int)y0, (int)x1, (int)y1);
            }
            else if (drawcolor.Equals("black"))
            {
                graphics.DrawLine(
                    Pens.Black,
                    (int)x0, (int)y0, (int)x1, (int)y1);
            }
            

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            th1 = trackBar3.Value * Math.PI / 180;
            label12.Text = trackBar3.Value.ToString();
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            per1 = (double)trackBar2.Value / (double)trackBar2.Maximum;
            label10.Text = per1.ToString();
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            th2 = trackBar5.Value* Math.PI / 180;
            label13.Text = trackBar5.Value.ToString();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            leng = 200 * trackBar1.Value / trackBar1.Maximum;
            label14.Text = leng.ToString();
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            per2 = (double)trackBar4.Value / (double)trackBar4.Maximum;
            label11.Text = per2.ToString(); 
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void label6_Click_1(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            n = int.Parse(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            color = comboBox1.Text;
        }
    }
}
